﻿using System;
using System.Data;
using YAF.Classes;
using YAF.Classes.Data;
using YAF.Core;
using YAF.Types;
using YAF.Types.Flags;
using YAF.Types.Interfaces;

namespace YAF.controls
{
    public partial class TopNews : BaseUserControl
    {
        /// <summary>
        ///     论坛板块ID
        /// </summary>
        public int ForumId
        {
            set { PageContext.Page["ForumID"] = value; }
        }

        /// <summary>
        ///     论坛分类ID
        /// </summary>
        public int CategoryID
        {
            set { PageContext.Page["CategoryID"] = value; }
        }

        private int baseSize = 10;
        

        #region Constants and Fields

        /// <summary>
        ///     The _forum.
        /// </summary>
        private DataRow forum;

        #endregion

        #region Constructors and Destructors

        #endregion

        #region Properties

        /// <summary>
        ///     Gets or sets the last post image TT.
        /// </summary>
        /// <value>
        ///     The last post image TT.
        /// </value>
        public string LastPostImageTt { get; set; }

        public int BaseSize
        {
            get { return baseSize; }
            set { baseSize = value; }
        }

        #endregion

        #region Public Methods

        /// <summary>
        ///     The style transform func wrap.
        /// </summary>
        /// <param name="dt">
        ///     The DateTable
        /// </param>
        /// <returns>
        ///     The style transform wrap.
        /// </returns>
        public DataTable StyleTransformDataTable([NotNull] DataTable dt)
        {
            if (this.Get<YafBoardSettings>().UseStyledNicks)
            {
                var styleTransform = this.Get<IStyleTransform>();
                styleTransform.DecodeStyleByTable(ref dt, true, "StarterStyle", "LastUserStyle");
            }

            return dt;
        }

        #endregion

        #region Methods

        /// <summary>
        ///     Gets the sub forum title.
        /// </summary>
        /// <returns>The get sub forum title.</returns>
        protected string GetSubForumTitle()
        {
            return this.GetTextFormatted("SUBFORUMS", this.HtmlEncode(PageContext.PageForumName));
        }

        /// <summary>
        ///     The initialization script for the topics page.
        /// </summary>
        /// <param name="e">
        ///     The EventArgs object for the topics page.
        /// </param>
        protected override void OnInit([NotNull] EventArgs e)
        {
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            base.OnInit(e);
        }

        /// <summary>
        ///     The page_ load.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        ///     The <see cref="System.EventArgs" /> instance containing the event data.
        /// </param>
        protected void Page_Load([NotNull] object sender, [NotNull] EventArgs e)
        {
            //if (this.PageContext.IsGuest && !this.PageContext.ForumReadAccess)
            //{
            //// attempt to get permission by redirecting to login...
            //this.Get<IPermissions>().HandleRequest(ViewPermissions.RegisteredUsers);
            //}
            //else if (!this.PageContext.ForumReadAccess)
            // {
            // YafBuildLink.AccessDenied();
            //}
            using (DataTable dt = LegacyDb.forum_list(PageContext.PageBoardID, PageContext.PageForumID))
            {
                forum = dt.Rows[0];
            }
            if (forum["RemoteURL"] != DBNull.Value)
            {
                Response.Clear();
                Response.Redirect((string)forum["RemoteURL"]);
            }
            new ForumFlags(forum["Flags"]);

            PageTitle.Text = this.HtmlEncode(forum["Name"]);

            BindData(); // Always because of yaf:TopicLine
        }


        /// <summary>
        ///     The bind data.
        /// </summary>
        private void BindData()
        {
            int? userId = null;

            // get the userID to use for the deleted posts count...
            if (!this.Get<YafBoardSettings>().ShowDeletedMessagesToAll)
            {
                // only show deleted messages that belong to this user if they are not admin/mod
                if (!PageContext.IsAdmin && !PageContext.ForumModeratorAccess)
                {
                    userId = PageContext.PageUserID;
                }
            }

            int nCurrentPageIndex = 0;

            DataTable dtTopics = LegacyDb.topic_list(
                PageContext.PageForumID,
                userId,
                DateTime.MinValue.AddYears(1754),
                DateTime.UtcNow,
                nCurrentPageIndex,
                BaseSize,
                this.Get<YafBoardSettings>().UseStyledNicks,
                true,
                this.Get<YafBoardSettings>().UseReadTrackingByDatabase);
            if (dtTopics != null)
            {
                dtTopics = StyleTransformDataTable(dtTopics);
            }
            if (dtTopics.Rows.Count == 0)
            {
                phContent.Visible = false;
            }
            else
            {
                TopicList.DataSource = dtTopics;
                DataBind();    
            }
        }

        /// <summary>
        ///     The Topics unload.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">
        ///     The <see cref="System.EventArgs" /> instance containing the event data.
        /// </param>
        private void Topics_Unload([NotNull] object sender, [NotNull] EventArgs e)
        {
            if (this.Get<IYafSession>().UnreadTopics == 0)
            {
                this.Get<IReadTrackCurrentUser>().SetForumRead(PageContext.PageForumID);
            }
        }

        #endregion
    }
}